/*
 * 前端资讯数据采集程序
 * */
require('colors')
const request = require('request-promise')
const schedule = require('node-schedule')
const logger = require('./config/log4j')
const {PROXY_PROVIDER_URL} = require('./config')
const freelanceSpider = require('./spider/freelance')
const zhaopinSpider = require('./spider/zhaopin')

/**
 * 获取代理ip地址列表
 * */
const get_proxy_list = async () => {
  logger.info('开始获取代理IP列表')
  let error
  const response = await request
    .get({
      url: PROXY_PROVIDER_URL
    })
    .catch(err => {
      error = err
    })

  logger.info('获取代理IP列表结束')
  let result = JSON.parse(response)

  if (result['code'] !== 0) {
    logger.error('获取代理ip出错,错误原因: ' + result['msg'])
    result = []
  } else {
    if (result['data']['count'] > 0) {
      result = result['data']['proxy_list']
    } else {
      result = []
    }
  }
  return new Promise((s, e) => {
    if (error) {
      e(error)
    }
    s(result)
  })
}

/**
 * 从代理ip列表中随机取一个ip
 * */
const get_random_ip = function(arr) {
  const randomIndex = Math.floor(Math.random() * arr.length)
  return arr[randomIndex]
}

const main = async () => {
  freelanceSpider()
  zhaopinSpider()
}

main().catch(err => {
  logger.error(err.red)
})

// let timesOne = 1
// let timesTwo = 1
// schedule.scheduleJob('*/3 * * * *', function () {
//   freelanceSpider()
//   console.log(`[项目]3/min, 第${timesOne++}次`.cyan)
// })
//
// schedule.scheduleJob('*/5 * * * *', function () {
//   zhaopinSpider()
//   console.log(`[招聘]5/min, 第${timesTwo++}次`.green)
// })
